/**
 * author: 何其沆(pooky)
 * desc: Vuex - Pool模块
 * date: 2021-11-15
 */
import dayjs from 'dayjs'
import { get, post } from '@/utils/request.js'
import api from '@/config/index.js'

export default {
  state: () => {
    return {
      // Account Pool列表
      poolTable: [],
      // Account Pool列表总数
      total: 0,
      // status === 11时数据
      lastProjectInfo: {},
      // status !== 11时数据
      clientPhaseInfo: {},
      // ------------------------------------
      // 管理端Account Pool列表
      adminPoolTable: [],
      // 管理端Account Pool列表总数
      adminTotal: 0,
      // 管理端status === 11时数据
      adminLastProjectInfo: {},
      // 管理端status !== 11时数据
      adminClientPhaseInfo: {}
    }
  },
  mutations: {
    /**
     * Account Pool列表
     * @param state
     * @param poolTable
     */
    POOL_TABLE(state, poolTable) {
      state.poolTable = poolTable
    },
    /**
     * Account Pool列表总数
     * @param state
     * @param total
     * @constructor
     */
    TOTAL(state, total) {
      state.total = total
    },
    /**
     * status === 11时数据
     * @param state
     * @param lastProjectInfo
     */
    LAST_PROJECT_INFO(state, lastProjectInfo) {
      state.lastProjectInfo = lastProjectInfo
    },
    /**
     * status !== 11时数据
     * @param state
     * @param clientPhaseInfo
     */
    CLIENT_PHASE_INFO(state, clientPhaseInfo) {
      state.clientPhaseInfo = clientPhaseInfo
    },
    /**
     * 管理端Account Pool列表
     * @param state
     * @param adminPoolTable
     * @constructor
     */
    ADMIN_POOL_TABLE(state, adminPoolTable) {
      state.adminPoolTable = adminPoolTable
    },
    /**
     * 管理端Account Pool列表总数
     * @param state
     * @param adminTotal
     * @constructor
     */
    ADMIN_TOTAL(state, adminTotal) {
      state.adminTotal = adminTotal
    },
    /**
     * 管理端status === 11时数据
     * @param state
     * @param adminLastProjectInfo
     * @constructor
     */
    ADMIN_LAST_PROJECT_INFO(state, adminLastProjectInfo) {
      state.adminLastProjectInfo = adminLastProjectInfo
    },
    /**
     * 管理端status !== 11时数据
     * @param state
     * @param adminClientPhaseInfo
     * @constructor
     */
    ADMIN_CLIENT_PHASE_INFO(state, adminClientPhaseInfo) {
      state.adminClientPhaseInfo = adminClientPhaseInfo
    }
  },
  actions: {
    /**
     * Account Pool列表
     * @param commit
     * @param data
     * @returns {Promise<void>}
     */
    async setPoolTable({ commit }, data) {
      await post(api.API_VERSION + '/Pool/SearchAccountPool', data, false).then(res => {
        if (res.code === 2000) {
          if (res.data) {
            const result = res.data
            for (let i = 0; i < result.length; i++) {
              const tags = []
              const item = result[i]
              if (item.status === 11) {
                if (item.isActive === true) {
                  tags.push('Active')
                } else {
                  tags.push('Inactive')
                }
              } else {
                tags.push(item.clientPhaseName)
              }
              tags.push(item.clientKindName.slice(0, 1))
              if (item.businessDirectionName.startsWith('Lo')) {
                tags.push('L10n')
              } else if (item.businessDirectionName.startsWith('AI')) {
                tags.push('Data')
              }
              if (item.fromGrade === 10) {
                tags.push('ECI')
              } else {
                tags.push('WT')
              }
              result[i].tags = tags
              result[i].key = i
              // isReconnect 表示二次开发中的客户
              if (item.status === 11 && !item.isReconnect) {
                result[i].expiryDateAdd = '--'
              } else {
                if (item.expiryDate) {
                  result[i].expiryDateAdd = dayjs(item.expiryDate).format('YYYY-MM-DD')
                } else {
                  result[i].expiryDateAdd = '--'
                }
              }
              if (item.turnPositiveOn) {
                result[i].turnPositiveOnAdd = dayjs(item.turnPositiveOn).format('YYYY-MM-DD')
              } else {
                result[i].turnPositiveOnAdd = '--'
              }
            }
            commit('POOL_TABLE', res.data)
            commit('TOTAL', res.total)
          }
        }
      })
    },
    /**
     * status === 11时数据
     * @param commit
     * @param data
     * @returns {Promise<void>}
     */
    async setLastProjectInfo({ commit }, data) {
      await get(api.API_VERSION + '/Pool/GetClientLastProjectInfo', data, false).then(res => {
        if (res.code === 2000) {
          if (res.data) {
            const result = res.data.list
            for (let i = 0; i < result.length; i++) {
              const item = result[i]
              if (item.createOn) {
                item.createOnAdd = dayjs(item.createOn).format('YYYY-MM-DD')
              } else {
                item.createOnAdd = '--'
              }
            }
            commit('LAST_PROJECT_INFO', res.data)
          }
        }
      })
    },
    /**
     * status !== 11时数据
     * @param commit
     * @param data
     * @returns {Promise<void>}
     */
    async setClientPhaseInfo({ commit }, data) {
      await get(api.API_VERSION + '/Pool/GetClientPhaseInfo', data, false).then(res => {
        if (res.code === 2000) {
          if (res.data && res.data.length > 0) {
            const result = res.data[0].clientPhareList
            for (let i = 0; i < result.length; i++) {
              const item = result[i]
              if (item.operateOn) {
                item.entryDateAdd = dayjs(item.operateOn).format('YYYY-MM-DD')
              } else {
                item.entryDateAdd = '--'
              }
            }
          }
          commit('CLIENT_PHASE_INFO', res.data)
        }
      })
    },
    /**
     * 管理端Account Pool列表
     * @param commit
     * @param data
     * @returns {Promise<void>}
     */
    async setAdminPoolTable({ commit }, data) {
      await post(api.API_VERSION + '/Admin/Pool/SearchAccountPool', data, false).then(res => {
        if (res.code === 2000) {
          if (res.data) {
            const result = res.data
            for (let i = 0; i < result.length; i++) {
              const tags = []
              const item = result[i]
              if (item.status === 11) {
                if (item.isActive === true) {
                  tags.push('Active')
                } else {
                  tags.push('Inactive')
                }
              } else {
                tags.push(item.clientPhaseName)
              }
              tags.push(item.clientKindName.slice(0, 1))
              if (item.businessDirectionName.startsWith('Lo')) {
                tags.push('L10n')
              } else if (item.businessDirectionName.startsWith('AI')) {
                tags.push('Data')
              }
              if (item.fromGrade === 10) {
                tags.push('ECI')
              } else {
                tags.push('WT')
              }
              result[i].tags = tags
              result[i].key = i
              if (item.status === 11) {
                result[i].expiryDateAdd = '--'
              } else {
                if (item.expiryDate) {
                  result[i].expiryDateAdd = dayjs(item.expiryDate).format('YYYY-MM-DD')
                } else {
                  result[i].expiryDateAdd = '--'
                }
              }
              if (item.turnPositiveOn) {
                result[i].turnPositiveOnAdd = dayjs(item.turnPositiveOn).format('YYYY-MM-DD')
              } else {
                result[i].turnPositiveOnAdd = '--'
              }
            }
            commit('ADMIN_POOL_TABLE', res.data)
            commit('ADMIN_TOTAL', res.total)
          }
        }
      })
    },
    /**
     * 管理端status === 11时数据
     * @param commit
     * @param data
     * @returns {Promise<void>}
     */
    async setAdminLastProjectInfo({ commit }, data) {
      await get(api.API_VERSION + '/Admin/Pool/GetClientLastProjectInfo', data, false).then(res => {
        if (res.code === 2000) {
          if (res.data) {
            const result = res.data.list
            for (let i = 0; i < result.length; i++) {
              const item = result[i]
              if (item.createOn) {
                item.createOnAdd = dayjs(item.createOn).format('YYYY-MM-DD')
              } else {
                item.createOnAdd = '--'
              }
            }
            commit('ADMIN_LAST_PROJECT_INFO', res.data)
          }
        }
      })
    },
    /**
     * 管理端status !== 11时数据
     * @param commit
     * @param data
     * @returns {Promise<void>}
     */
    async setAdminClientPhaseInfo({ commit }, data) {
      await get(api.API_VERSION + '/Admin/Pool/GetClientPhaseInfo', data, false).then(res => {
        if (res.code === 2000) {
          if (res.data && res.data.length > 0) {
            const result = res.data[0].clientPhareList
            for (let i = 0; i < result.length; i++) {
              const item = result[i]
              if (item.operateOn) {
                item.entryDateAdd = dayjs(item.operateOn).format('YYYY-MM-DD')
              } else {
                item.entryDateAdd = '--'
              }
            }
          }
          commit('ADMIN_CLIENT_PHASE_INFO', res.data)
        }
      })
    }
  }
}
